from django.test import TestCase
import unittest,time
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from django.test.client import Client
from front import views
from front.models import CommDisaster

# 初始页面测试
class IndexPageTest(unittest.TestCase):

    def setUp(self):
        print('测试环境')
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = ""
        self.verificationErrors = []
        self.accept_next_alert = True
        self.client = Client()



    def testPOSTRequest(self):
        print('测试中')
        driver = self.driver
        response = driver.get("http://127.0.0.1:8000/")
        self.driver.implicitly_wait(30)
        parentMenu = driver.find_element_by_id("showList")
        # 鼠标悬停在一级菜单上
        ActionChains(driver).move_to_element(parentMenu).perform()
        time.sleep(10)
        # 点击二级菜单按钮
        driver.find_element_by_name("336").click()
        # response = self.client.get('/')
        self.assertEqual(response.status_code, 200)


    def tearDown(self):
        sleep(2)
        print('测试销毁')
        self.driver.quit()


# 数据源编码是否为202
class dataSourceCodeTest(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()

    def test_match_string(self):
        browser = self.browser
        browser.get("http://127.0.0.1:8000/")
        string = browser.find_element_by_name("202")
        result = views.dataSourceCode(string)
        self.assertEqual(result, 202)    # 返回值是否为202

    def tearDown(self):
        self.browser.quit()

# 数据库测试
class CommDisasterModelTest(TestCase):
    def test_saving_and_retrieving_data(self):
        first_data =CommDisaster()
        first_data.Code = '3700000000003366789'
        first_data.Date='20200502'
        first_data.Location='山东省'
        first_data.Type='336'
        first_data.Grade=1
        first_data.Picture='test1'
        first_data.Note='nothing happened'
        first_data.ReportingUnit='202-四川省'
        first_data.SequenceNumber='6789'
        first_data.save()

        second_data = CommDisaster()
        second_data.Code = '3701000000003364553'
        second_data.Date = '20190714'
        second_data.Location='山东省济南市'
        second_data.Type='336'
        second_data.Grade=2
        second_data.Picture='test2'
        second_data.Note='nothing happened'
        second_data.ReportingUnit='202-北京市'
        second_data.SequenceNumber='4553'
        second_data.save()

        saved_items = CommDisaster.objects.all()
        self.assertEqual(saved_items.count(),2)

        first_saved_item = saved_items[0]
        second_saved_item = saved_items[1]
        self.assertEqual(first_saved_item.Code, '3700000000003366789')
        self.assertEqual(first_saved_item.Date, '20200502')
        self.assertEqual(first_saved_item.Location, '山东省')
        self.assertEqual(first_saved_item.Type, '336')
        self.assertEqual(first_saved_item.Grade, 1)
        self.assertEqual(first_saved_item.Picture, 'test1')
        self.assertEqual(first_saved_item.Note, 'nothing happened')
        self.assertEqual(first_saved_item.ReportingUnit, '202-四川省')
        self.assertEqual(first_saved_item.SequenceNumber, '6789')

        self.assertEqual(second_saved_item.Code, '3701000000003364553')
        self.assertEqual(second_saved_item.Date, '20190714')
        self.assertEqual(second_saved_item.Location, '山东省济南市')
        self.assertEqual(second_saved_item.Type, '336')
        self.assertEqual(second_saved_item.Grade, 2)
        self.assertEqual(second_saved_item.Picture, 'test2')
        self.assertEqual(second_saved_item.Note,'nothing happened')
        self.assertEqual(second_saved_item.ReportingUnit, '202-北京市')
        self.assertEqual(second_saved_item.SequenceNumber, '4553')

# 数据展示测试
class DataShowTest(unittest.TestCase):
    
    def setUp(self):
        print('测试环境')
        self.browser = webdriver.Firefox()
        
    def test_database_from_string(self):
        browser = self.browser
        browser.get("http://127.0.0.1:8000/data/333")
        string = browser.find_element_by_id("type").get_attribute("textContent")
        self.assertEqual(string, "多源异构灾情数据综合管理系统_灾情信息:333展示")    # 数据库来源是否为333

    def tearDown(self):
        self.browser.quit()

#数据源对象测试
class DataSourceObjectTest(unittest.TestCase):

    def setUp(self):
        print('测试环境')
        self.browser = webdriver.Firefox()

    # 测试数据是否是json格式，如果不是json格式则抛出异常
    def test_is_json(myjson):
        try:
            json.loads(myjson)
        except ValueError:
            return False
        return True

    # 测试数据源的ID是否为19位编码
    def test_source_data_ID_number(ID_number):
        if (len(ID_number) == 19):
            return True
        else:
            return False

    def tearDown(self):
        self.browser.quit()

if __name__ == '__main__':
    unittest.main()
